/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package xmarti.vacaxm.client.maintenance.auxiliar;




import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import xmarti.vacaxm.action.ImplMaintenance;
import xmarti.vacaxm.beans.maintenance.Area;
import xmarti.vacaxm.exception.ExceptionHibernateVacaxm;
import xmarti.vacaxm.exception.ExceptionXML;
import xmarti.vacaxm.utils.JDialogXM;
import xmarti.vacaxm.utils.TDSLanguageUtils;
import xmarti.vacaxm.utils.TableModel;

/**
 *
 * @author xmarti
 */
public class JDArea extends JDialogXM{    
    private List<Area>arees;
    private Area area;
    private ImplMaintenance aMaintenance;
    private JPAuxiliarData1 jPData;
    public JDArea(){        
        super(null,true,"JdArea",TDSLanguageUtils.getMessage("jDialogArea.JDialog.caption"));        
        this.aMaintenance=new ImplMaintenance(); 
        this.jPData= new JPAuxiliarData1(true);
        super.getjPanelData().setViewportView(this.jPData);
        try {
            super.chargeTable();
        } catch (ExceptionHibernateVacaxm ex) {
            Logger.getLogger(JDArea.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExceptionXML ex) {
            Logger.getLogger(JDArea.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }

    @Override
    public TableModel getModel() throws ExceptionHibernateVacaxm {
       TableModel dadesTaula = null;                
            this.arees=(List<Area>) this.aMaintenance.getAreas();
            String [] columns={TDSLanguageUtils.getMessage("jDialogArea.JTable.name.caption"),                               
                          TDSLanguageUtils.getMessage("jDialogArea.JTable.description.caption"),                                                  
                          };    
            Object[][] dades=null;
            if (this.arees.size()==0){
                dades=new Object[1][columns.length];                          
                dades[0][0]=new Area();
                dades[0][1]="-";                                
            }
            else{
                dades=new Object[this.arees.size()][columns.length];                          
                Integer i=0;            
                for (Iterator it = this.arees.iterator(); it.hasNext();) {
                    Area c = (Area) it.next();                
                    dades[i][0]= c;                                        
                    if (c.getName()==(null))dades[i][1]="-"; else dades[i][1]= c.getName();                    
                    i++;
                }     
            }                  
        return new TableModel(columns, dades);   
    }

    @Override
    
    public void getData() {
        this.area.setName(this.jPData.getNameAuxiliar());
        this.area.setCode(this.jPData.getCodeAuxiliar());        
    }

    @Override
    public void setData() {
       if(super.isInsert()) this.area=new Area();
       else this.area=(Area) super.getObject();
       this.jPData.setNameAuxiliar(this.area.getName());
       this.jPData.setCodeAuxiliar(this.area.getCode());                 
    }
    @Override
    public void saveData() throws ExceptionHibernateVacaxm {
        getData();              
        if (this.area.getName().length()>0){
            if (super.isInsert()){
                this.area.setId(this.aMaintenance.add(this.area));
                super.setObject(this.area);                
            }else{
                this.aMaintenance.update(this.area);                
            }            
        }else{
            throw new ExceptionHibernateVacaxm(ExceptionHibernateVacaxm.ERR_INSERT_NAME_NULL);
        }
    }
    @Override
    public void removeData() throws ExceptionHibernateVacaxm {
        this.aMaintenance.delete(this.area);
        super.setObject(new Area());
    }

    @Override
    public boolean isSaved() {        
        if(this.area==null) return true;
        if (!this.area.getName().equals(this.jPData.getNameAuxiliar())) return false;        
        if(!this.area.getCode().equals(this.jPData.getCodeAuxiliar())) return false; 
        return true;
    }
    
}
